package com.kira.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kira.back.model.domain.User;
import com.kira.back.service.UserService;
import com.kira.back.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.kira.back.contant.UserContant.USER_LOGIN_STATE;


/**
* @author Administrator
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-03-20 15:35:08
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;

    /**
     * 盐值，混淆密码
     */
    public static final String SALT = "kira";


    @Override
    public long userRegister(String loginAccount, String loginPassword, String checkPassword) {
        // 不为空
        if (StringUtils.isAnyBlank(loginAccount, loginPassword, checkPassword)) {
            // todo 修改为自定义异常
            return -1;
        }
        // 用户名长度
        if (loginAccount.length() < 4) {
            return -1;
        }
        // 账号不能包含特殊字符
        String validPattern = "^[a-zA-Z0-9_-]+$";
        if (!loginAccount.matches(validPattern)) {
            return -1;
        }
        // 密码和校验密码相同
        if (!loginPassword.equals(checkPassword)) {
            return -1;
        }
        // 账号不能重复
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginAccount", loginAccount);
        long count = this.count(queryWrapper);
        if (count > 0) {
            return -1;
        }
        // 加密
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+ loginPassword).getBytes());
        // 插入数据
        User user = new User();
        user.setLoginAccount(loginAccount);
        user.setLoginPassword(encryptPassword);
        boolean saveResult = this.save(user);
        if (!saveResult) {
            return -1;
        }
        return user.getId();
    }

    @Override
    public User userLogin(String loginAccount, String loginPassword, HttpServletRequest request) {
        // 不为空
        if (StringUtils.isAnyBlank(loginAccount, loginPassword)) {
            return null;
        }
        // 用户名长度
        if (loginAccount.length() < 4) {
            return null;
        }
        // 账号不能包含特殊字符
        String validPattern = "^[a-zA-Z0-9_-]+$";
        if (!loginAccount.matches(validPattern)) {
            return null;
        }
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+ loginPassword).getBytes());
        // 查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loginAccount", loginAccount);
        queryWrapper.eq("loginPassword", encryptPassword);
        User user = userMapper.selectOne(queryWrapper);
        // 用户不存在
        if (user == null) {
            log.info("user login failed, loginAccount cannot match loginPassword");
            return null;
        }
        // 脱敏
        User safetUser = getSafetyUser(user);
        // 记录登录态
        request.getSession().setAttribute(USER_LOGIN_STATE, safetUser);

        return safetUser;
    }

    /**
     * 用户脱敏
     * @param originUser
     * @return
     */
    @Override
    public User getSafetyUser(User originUser) {
        if (originUser == null) {
            return null;
        }
        User safetUser = new User();

        safetUser.setId(originUser.getId());
        safetUser.setUsername(originUser.getUsername());
        safetUser.setEmail(originUser.getEmail());
        safetUser.setPhone(originUser.getPhone());
        safetUser.setAvatarUrl(originUser.getAvatarUrl());
        safetUser.setGender(originUser.getGender());
        safetUser.setUserRole(originUser.getUserRole());
        safetUser.setIsDelete(originUser.getIsDelete());

        return safetUser;
    }
}




